<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <div id="echarts">
      </div>
    </el-card>
  </div>
</template>

<script>
// https://echarts.apache.org/zh/index.html
import _ from 'lodash'
const echarts = require('echarts')

export default {
  data () {
    return {
      options: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          { boundaryGap: false }
        ],
        yAxis: [
          { type: 'value' }
        ]
      }
    }
  },
  mounted () {
    // dom 加载完成后，再初始化图表
    this.getReport()
  },
  methods: {
    async getReport () {
      const { data } = await this.$http.get('reports/type/1')
      if (data.meta.status !== 200) {
        this.$message.error('获取图表数据失败，' + data.meta.msg)
      }
      console.log(data)
      this.options = _.merge(this.options, data.data) // 深度合并
      echarts.init(document.getElementById('echarts')).setOption(this.options)
    }
  }
}
</script>

<style lang="less" scoped>
#echarts {
  width: 650px;
  height: 400px;
}
</style>
